<?php

namespace App\Http\Controllers\CRM;

use App\Http\Controllers\Controller;
use App\Http\Requests\CRM\GoodsRequest;
use App\Http\Resources\CRM\GoodsResource;
use App\Models\Goods;
use Illuminate\Http\Request;

class GoodsController extends Controller
{
    public function create(GoodsRequest $request)
    {
        $goods = Goods::create($request->only([
            'category_id', 'name', 'price', 'original_price', 'unit', 'purchase_cost_price',
            'sale_cost_price', 'specification_status', 'specifications', 'head_image', 'images', 'point', 'code',
        ]));

        return new GoodsResource($goods);
    }

    public function edit(Goods $goods, GoodsRequest $request)
    {
        $goods->update($request->only([
            'category_id', 'name', 'price', 'original_price', 'unit', 'purchase_cost_price',
            'sale_cost_price', 'specification_status', 'specifications', 'head_image', 'images', 'point', 'code',
        ]));

        return new GoodsResource($goods);
    }

    public function remove(Goods $goods)
    {
        $goods->delete();

        return response()->noContent();
    }

    public function getInfo(Goods $goods, Request $request)
    {
        $goods->load(['stocks' => function ($query) use($request) {
            $query->where('store_id', $request->store_id);
        }]);

        return new GoodsResource($goods);
    }
}
